package com.baitiaojun.user.api.impl;

import com.baitiaojun.api.annotation.UserAuthoritiesAccess;
import com.baitiaojun.api.domain.user.UserFollowing;
import com.baitiaojun.api.util.ConstantUtil;
import com.baitiaojun.api.vo.JsonRes;
import com.baitiaojun.user.api.UserFansApi;
import com.baitiaojun.user.service.UserFansService;
import com.baitiaojun.user.support.UserSupport;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @类名 UserFansApiImpl
 * @描述 TODO
 * @作者 白条君
 * @创建日期 2023/1/15 17:44
 * @版本 1.0
 */
@RestController
public class UserFansApiImpl implements UserFansApi {

    @Resource
    private UserFansService userFansService;

    @Resource
    private UserSupport userSupport;

    @Resource
    private HttpServletRequest request;

    @Resource
    private HttpServletResponse response;

    @UserAuthoritiesAccess(
            accessRole = {ConstantUtil.ROLE_LV0
                    ,ConstantUtil.ROLE_LV1
                    ,ConstantUtil.ROLE_LV2
                    ,ConstantUtil.ROLE_LV3
                    ,ConstantUtil.ROLE_LV4
                    ,ConstantUtil.ROLE_LV5
                    ,ConstantUtil.ROLE_LV6
            })
    @Override
    public JsonRes getFans() throws Exception {
        Long currentUserId = userSupport.getCurrentUserId(request);
        String token = userSupport.getToken(request);
        Object fans = userFansService.getFans(token, UserFollowing.builder().userid(currentUserId).build());
        response.setHeader("Authorization", userSupport.getToken(request));
        return JsonRes.builder().data(fans);
    }

    @Override
    public JsonRes getFans(Long userId) throws Exception {
        Map<String, Object> fans = userFansService.getFans(UserFollowing.builder().userid(userId).build());
        return JsonRes.builder().data(fans.get("fans"));
    }

    public JsonRes getFansList(Long userId) throws Exception {
        Map<String, Object> fansList = userFansService.getFansList(UserFollowing.builder().userid(userId).build());
        return JsonRes.builder().data(fansList.get("fans"));
    }
}